"use strict";

const Controller = require("egg").Controller;
const OSS = require("ali-oss");

const OSSPage = new OSS({
  region: "oss-cn-beijing",
  bucket: "zkx123",
  accessKeyId: "LTAI5tMh2kZgn54CDt8jKhxE",
  accessKeySecret: "2hZu31Wi11ocNJMafHQ77nGrWoVjUn",
});

class UploadController extends Controller {
  async imgUpLoad() {
    const { ctx } = this;

    // 获取文件流信息
    console.log(ctx.request.files);
    const file = ctx.request.files[0];

    // 随机生成文件名称
    const filename = new Date().getTime() + file.filename;

    // 将文件保存到oss中
    const data = await OSSPage.put(filename, file.filepath);

    ctx.body = {
      code: 1,
      data,
    };
  }
}

module.exports = UploadController;
